package com.tangbuzhi.chm.helper;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.helper.ItemTouchHelper;

/**
 * http://git.oschina.net/tangbuzhi
 *
 * @author Tangbuzhi
 * @version V2.0
 * @Package com.tangbuzhi.chm.helper
 * @Description: ${TODO}
 * @date: 2017/9/11
 */
public class TouchCallback extends ItemTouchHelper.Callback {

    /*手指滑动方向=>up、down、left、right*/
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        int dragFlags;
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager || manager instanceof StaggeredGridLayoutManager) {
            //当recyclerview的布局管理器是网格布局或瀑布流管理器时，可以全方位拖动
            //注意，GridLayoutManager是集成自LinearLayoutManager,所以在判断时，尽可能不用它
            dragFlags = ItemTouchHelper.LEFT | ItemTouchHelper.UP | ItemTouchHelper.RIGHT | ItemTouchHelper.DOWN;
        } else {
            dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        }
        // 如果想支持滑动(删除)操作, swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END
        int swipeFlags = 0;
        return makeMovementFlags(dragFlags, swipeFlags);
    }

    /*处理拖动事件*/
    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        // 不同Type之间不可移动
        if (viewHolder.getItemViewType() != target.getItemViewType()) {
            return false;
        }
        //处理drag事件
        if (recyclerView.getAdapter() instanceof OnItemMoveListener) {
            OnItemMoveListener listener = (OnItemMoveListener) recyclerView.getAdapter();
            listener.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
            return true;
        }
        return false;
    }

    /*处理横向滑动事件*/
    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        //to do nothing
    }

    /*选中状态改变时*/
    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        //不在闲置状态
        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
            if (viewHolder instanceof OnItemDragListener) {
                OnItemDragListener listener = (OnItemDragListener) viewHolder;
                listener.onItemDragStart();
            }
        }
        super.onSelectedChanged(viewHolder, actionState);
    }

    /*从recyclerview中删除view*/
    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        if (viewHolder instanceof OnItemDragListener) {
            OnItemDragListener listener = (OnItemDragListener) viewHolder;
            listener.onItemDragFinish();
        }
        super.clearView(recyclerView, viewHolder);
    }

    /*是否支持长按拖动*/
    @Override
    public boolean isLongPressDragEnabled() {
        //return super.isLongPressDragEnabled();
        return false;
    }

    /*是否支持左右滑动*/
    @Override
    public boolean isItemViewSwipeEnabled() {
        //return super.isItemViewSwipeEnabled();
        return false;
    }
}
